package com.frx.search.binarysearch;

/**
 * @author ：frx
 * @date ：Created in 2019/10/28 22:24
 * @description：ToDO
 * @modified By：
 * @version: ToDO
 */
public class TestOne {

    public static void main(String[] args){
        int[] arr = {1,2,3,4,5,6,7,8,9};
        System.out.println(search(arr,5));
        System.out.println(find(arr,6,0,arr.length - 1));
    }
    public static int search(int[] arr,int target){
        int low = 0;
        int high = arr.length - 1;
        while (low <= high){
            int mid = low + ((high - low) >> 1);
            if (arr[mid] == target){
                return mid;
            } else if (arr[mid] < target){
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }

        return -1;
    }

    public static int find(int[] arr,int val,int low,int high){
        if (low > high){
            return -1;
        }
        int mid = low + ((high - low)>>1);
        if (arr[mid] == val){
            return mid;
        } else if (arr[mid] < val){
            return find(arr,val,mid + 1,high);
        } else {
            return find(arr,val,low,mid - 1);
        }

    }


}
